package com.itheima.reggie.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Page;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.exception.BusinessException;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.SetmealDishMapper;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired(required = false)
    private SetmealMapper setmealMapper;

    @Autowired(required = false)
    private SetmealDishMapper setmealDishMapper;

    @Autowired(required = false)
    private CategoryMapper categoryMapper;

    @Transactional
    @Override
    @CacheEvict(value = "setmealCache",allEntries = true)
    public Boolean save(SetmealDto setmealDto) {
        setmealDto.setStatus(1);
        setmealDto.setUpdateTime(LocalDateTime.now());
        setmealDto.setCreateTime(LocalDateTime.now());

        setmealMapper.save(setmealDto);

        List<SetmealDish> setmealDisheslist = setmealDto.getSetmealDishes();
        ArrayList<SetmealDish> setmealDishes = new ArrayList<>();
        for (SetmealDish setmealDish : setmealDisheslist) {
            setmealDish.setSetmealId(setmealDto.getId());
            setmealDish.setCreateUser(setmealDto.getCreateUser());
            setmealDish.setCreateTime(LocalDateTime.now());
            setmealDish.setUpdateUser(setmealDto.getUpdateUser());
            setmealDish.setUpdateTime(LocalDateTime.now());
            setmealDish.setSort(0);
            setmealDishes.add(setmealDish);
        }
        setmealDishMapper.save(setmealDishes);
        return true;
    }

    @Override
    public Page<SetmealDto> findByPage(Integer page, Integer pageSize, String name) {
        PageHelper.startPage(page, pageSize);
        List<Setmeal> setmealList = setmealMapper.findAll(name);
        PageInfo<Setmeal> pageinfo = new PageInfo<>(setmealList);
        ArrayList<SetmealDto> setmealDtoList = new ArrayList<>();
        for (Setmeal setmeal : setmealList) {
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(setmeal, setmealDto);
            Category category = categoryMapper.findById(setmealDto.getCategoryId());
            setmealDto.setCategoryName(category.getName());
            setmealDtoList.add(setmealDto);
        }
        Page<SetmealDto> pageObj = new Page<>();
        pageObj.setRecords(setmealDtoList);
        pageObj.setTotal(pageinfo.getTotal());
        return pageObj;
    }

    /**删除套餐业务方法
     * @param ids        要删除的套餐id列表数据
     * @param updateUser 更新人
     * @return
     */
    @Transactional
    @Override
    @CacheEvict(value = "setmealCache",allEntries = true)
    public boolean deleteByIds(List<Long> ids, Long updateUser) {
        //判断传递过来的套餐id列表查询 是否存在启用的套餐
        // 查询启动的个数，个数大于0代表存在 否则不存在
        Integer count = setmealMapper.countByIds(ids);
        //如果存在 抛出异常
        if(count>0){
            throw new BusinessException("无法删除启售的套餐");
        }
        //3.调用数据访问层进行批量删除 传递修改人 最新的修改时间
        //批量删除套餐菜品关系数据
        setmealDishMapper.deleteBySetmealIds(ids,updateUser,LocalDateTime.now());
        //批量删除套餐
        setmealMapper.deleteByIds(ids, updateUser, LocalDateTime.now());
        return true;
    }

    /**
     * 根据套餐的类别展示套餐
     * @param categoryId
     * @param status
     * @return
     */
    @Override
    @Cacheable(value="setmealCache",key="#categoryId+''+#status",unless = "#result==null")
    public List<Setmeal> list(Long categoryId, Integer status) {
        return setmealDishMapper.list(categoryId,status);
    }

    @Override
    public SetmealDto findById(Long id) {
       SetmealDto setmealDto = setmealMapper.findById(id);
        return setmealDto;
    }

    @Transactional
    @Override
    @CacheEvict(value = "setmealCache",allEntries = true)
    public R<String> update(SetmealDto setmealDto) {
        setmealDto.setUpdateTime(LocalDateTime.now());
        setmealMapper.update(setmealDto);
        setmealDishMapper.delete(setmealDto.getId());

        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();

        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmealDto.getId());
            setmealDish.setSort(0);
            setmealDish.setCreateTime(LocalDateTime.now());
            setmealDish.setCreateUser(setmealDto.getCreateUser());
            setmealDish.setUpdateTime(LocalDateTime.now());
            setmealDish.setUpdateUser(setmealDto.getUpdateUser());
        }
        setmealDishMapper.insert(setmealDishes);
        return R.success("修改成功");
    }

}
